package com.pri.chat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.KeFuEntity;
import com.netease.nim.uikit.net.entity.LookMemberListBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.activity.OtherPeopleMsgActivity;
import com.pri.chat.adapter.LookMemberAdapter;
import com.pri.chat.adapter.SelectSignAdapter;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.utils.Des3Util;
import com.pri.chat.utils.StringUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2020/5/9
 */
public class SelectSignActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    @BindView(R.id.et_search)
    EditText et_search;
    @BindView(R.id.searchBtn)
    TextView searchBtn;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_sign_layout;
    }

    @Override
    protected void initView() {
        setTitle("选择标签");
        initLayout();
        initSrl();
    }

    @Override
    protected void initData() {
        searchData();
    }

    private List<KeFuEntity> mList;
    private SelectSignAdapter mAdapter;
    private int page = 1;
    private boolean isShowDialog = false;

    private void initLayout() {
        mList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recMain.setLayoutManager(linearLayoutManager);
        mAdapter = new SelectSignAdapter(R.layout.item_select_sign, mList);
        recMain.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    private void searchData() {
        SubscriberOnNextListener<BaseBean<List<KeFuEntity>>> onNextListener = baseBean -> {
            if (baseBean.getData().isEmpty() && page == 1) {
                if(!StringUtils.isEmpty(et_search.getText().toString().trim())){
                    KeFuEntity kf = new KeFuEntity();
                    kf.setValue(et_search.getText().toString());
                    kf.setTitle("使用#"+et_search.getText().toString()+"#");
                    mList.add(kf);
                }
                mAdapter.notifyDataSetChanged();
            } else {
                mList = baseBean.getData();
                if(!StringUtils.isEmpty(et_search.getText().toString().trim())){
                    boolean add = true;
                    KeFuEntity selec = null;
                    for(int i=0;i<mList.size();i++){
                        if(mList.get(i).getValue().equals(et_search.getText().toString())){
                            add = false;
                            selec = mList.get(i);
                            break;
                        }
                    }
                    if(add){
                        KeFuEntity kf = new KeFuEntity();
                        kf.setValue(et_search.getText().toString());
                        kf.setTitle("使用#"+et_search.getText().toString()+"#");
                        mList.add(kf);
                    }else{
                        mList.remove(selec);
                        mList.add(0,selec);
                    }
                }
                mAdapter.setNewData(mList);
            }
        };
        Map<String, Object> dtModel = new HashMap<>();//登录上传参数
        dtModel.put("memberId", et_search.getText().toString());
        dtModel.put("name", et_search.getText().toString());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(dtModel));
        HttpMethods.getInstance().getSignList(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
        srlAll.setOnLoadmoreListener(refresh_layout -> {
            refresh_layout.finishLoadmore(1000);
            isShowDialog = false;
            page++;
            searchData();
        });
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent();
        intent.putExtra("sign","#"+mList.get(position).getValue()+"#");
        setResult(RESULT_OK,intent);
        finish();
    }

    private void refresh() {
        page = 1;
        isShowDialog = false;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        searchData();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("id", mList.get(position).getId());
        RxActivityTool.skipActivity(this, OtherPeopleMsgActivity.class, bundle);
    }

    @OnClick({R.id.searchBtn})
    public void onClick(View v) {
        if (v.getId() == R.id.searchBtn) {
            if (et_search.getText().toString().equals("")) {
                RxToast.showToast("请输入搜索内容");
                return;
            }
            refresh();
        }
    }
}
